/* Copyright (c) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.orkut.client.api.samples.greetings.client.ui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.orkut.client.api.samples.greetings.client.GreetingsMessages;

/**
 * A dialog box that confirms that a message was sent.
 *
 * @author Shishir Birmiwal
 */
@Singleton
public class ScrapSentDialogBox extends DialogBox {
  private final GreetingsMessages msgs;
  private final Label message;
  private final Button startOverButton;
  private final Button sendAgainButton;

  public static interface ScrapSendDialogBoxCallback {
    public void startOver();
    public void sendAgain();
  }

  private ScrapSendDialogBoxCallback cb;

  @Inject
  public ScrapSentDialogBox(GreetingsMessages msgs,
      Label message, Button startOverButton, Button sendAgainButton) {
    this.msgs = msgs;
    this.message = message;
    this.startOverButton = startOverButton;
    this.sendAgainButton = sendAgainButton;
    setupWidgets();
  }

  public void setCallback(ScrapSendDialogBoxCallback cb) {
    this.cb = cb;
  }

  private void setupWidgets() {
    message.setText(msgs.successfullySentMessage());
    startOverButton.setText(msgs.startOver());
    sendAgainButton.setText(msgs.sendAgain());
    HorizontalPanel panel = new HorizontalPanel();
    panel.add(startOverButton);
    panel.add(sendAgainButton);
    FlowPanel flowPanel = new FlowPanel();
    flowPanel.add(message);
    flowPanel.add(panel);
    this.add(flowPanel);
    this.setText(msgs.successTitle());
    this.setGlassEnabled(true);

    startOverButton.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent arg0) {
        if (cb != null) {
          cb.startOver();
        }
        hide();
      }
    });
    sendAgainButton.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent arg0) {
        if (cb != null) {
          cb.sendAgain();
        }
        hide();
      }
    });
  }
}
